<?php
class User_entry_quit_model extends HYD_Model {
    private $_sql_where = '';
    
    public function __construct(){
        $this->set_table_name('user_entry_quit');
    }


    /**
     * 获取用户状态记录
     *
     * 根据ID获取指定一条管理员记录
     * @param	 string id 权限ID
     * @return	 array 结果集
     */
    public function get_data($userid,$type) {
        $feild_list = $this->get_all_feilds();
        $where['userid'] = $userid;
        $where['otype'] = $type;
        $result_data = $this->db
                ->select($feild_list)
                ->from($this->get_table_name())
                ->where($where)
                ->order_by("id", "desc")
                ->get();
        return $result_data->row_array();
    }
    
    /**
     * 保存用户数据
     *
     * 更新或插入用户数据,$id为NULL时插入，反之更新
     * @param	string $positionid 岗位ID
     * @param	string id 用户ID
     * @param	array $data 数据
     * @return	integer  1=成功  -3=用户名手机号已经存在  -4=未提供岗位ID   MODEL_RETURN_DBERROR=数据库错误
     */
    public function save($id = NULL, $data) {
        $feild_array = array('id_value' => $id);
        //检查用户名是否重复   
        if (isset($data['username']) && $data['username'] != '') {
            $feild_array['where'] = array('username' => $data['username']);
            if ($this->get_is_duplicate($feild_array)) {
                return -3;
            }
        }

        //事务开始
        $this->db->trans_begin();

        $row_data = $this->get_update_feilds($data);
        if (isset($data['password']) && !empty($data['password'])) {
            $row_data['password'] = $this->get_encrypt_password(trim($data['password']));
        } else {
            unset($row_data['password']);
        }
        if (is_numeric($id)) {
            $this->db->update($this->get_table_name(), $row_data, array('id' => $id));
        } else {
            unset($row_data['id']);
            $this->db->insert($this->get_table_name(), $row_data);
        }
        if ($this->check_dbtrans() === FALSE) {
            return MODEL_RETURN_DBERROR;
        }

        //事务结束
        $this->db->trans_commit();
        return MODEL_RETURN_SUCCESS;
    }
    
    /**
     * 获取数据列表
     *
     * 根据分页条件获取数据列表
     * @param	string $start 记录开始位置
     * @param	string $limit 记录数
     * @return	 array 结果集
     */
    public function get_lists($start = NULL, $limit = NULL, $sql_str = '', $root_show_flag = FALSE) {
        $feild_list = $this->get_all_feilds();
        $this->db->select($feild_list);
        $this->db->from($this->get_table_name());
        $where_str = '';
        if ($root_show_flag == FALSE) {
            $where_str = 'id>0';
        }
        //搜索处理      
        if (strlen($sql_str) > 0) {
            if ($where_str) {
                $where_str .= " and ( {$sql_str} )";
            } else {
                $where_str = $sql_str;
            }
        }

        if ($where_str) {
            $this->_sql_where = $where_str;
            $this->db->where($where_str, NULL, FALSE);
        }
        $this->db->order_by('id');

        if (!(is_null($start) && is_null($limit))) {
            $this->db->limit($limit, $start);
        }
        $result_list = $this->db->get();
        return $result_list->result_array();
    }

    /**
     * 获取记录总数
     * @return	 integer 
     */
    public function get_total() {
        if ($this->_sql_where){
            $this->db->where($this->_sql_where, NULL, FALSE);
        }
        return $this->db->count_all_results($this->get_table_name());
        //return $this->db->count_all($this->get_table_name());
    }

    
}